<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="dialogVisible=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList" border="">
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <!-- 作用域插槽 拿到每行的数据 -->
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignRole(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delBtn(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination :page-size="pageConfig.pagesize" :total="total" layout="prev,pager,next" @current-change="currentChange" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyInfo.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyInfo.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyInfo.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 对话框 -->
      <el-dialog
        :title="fromData.id?'编辑角色':'新增角色'"
        :visible.sync="dialogVisible"
        width="40%"
        @close="btnDel"
      >
        <el-form ref="formRole" :model="fromData" :rules="rules">
          <el-form-item prop="name" label="角色名称">
            <el-input v-model="fromData.name" />
          </el-form-item>
          <el-form-item prop="description" label="描述">
            <el-input v-model="fromData.description" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="small" type="primary" @click="btnOK">确 定</el-button>
          <el-button size="small" @click="btnDel">取 消</el-button>
        </span>
      </el-dialog>

      <!-- 给角色权限分配 -->
      <el-dialog title="设置角色权限" :visible="isShowPerm">
        <el-checkbox-group v-model="permIDs">
          <el-tree
            :data="permList"
            :props="{label: 'name'}"
            default-expand-all
          >
            <template v-slot="{data}">
              <el-checkbox :label="data.id">
                {{ data.name }}
              </el-checkbox>
            </template>
            <el-tree />
          </el-tree>
        </el-checkbox-group>
        <template #footer>
          <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, addRole, getRoleID, ediRole, delRole, assignPremRole } from '@/api/setting'
import { getPermList } from '@/api/permission'
import { listToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      roleId: '', // 角色id
      permList: [], // 权限列表
      permIDs: [], // 被选中的权限id数组
      isShowPerm: false, // 权限分配会话框
      companyInfo: {}, // 公司信息
      pageConfig: {
        page: 1, // 页码
        pagesize: 2 // 每页条数
      },
      roleList: [], // 角色列表
      total: 0, // 总条数
      dialogVisible: false, // 对话框默认隐藏
      fromData: { // 要传给后台的数据
        name: '',
        description: ''
      },
      rules: { // 效验
        name: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 获取公司信息
    this.companyInfo = await getCompanyInfo(this.$store.getters.companyId)
    // console.log(this.companyInfo)

    // 获取角色列表请求
    this.getRoleListFn()
    // 获取权限
    this.permList = listToTreeData(await getPermList(), '0')
  },
  methods: {
    // 权限确定
    async btnPermOK() {
      await assignPremRole({
        id: this.roleId,
        permIds: this.permIDs
      })
      this.$message.success('操作成功')
      this.isShowPerm = false
    },
    // 权限取消
    btnPermCancel() {
      this.permIDs = []
      this.isShowPerm = false
    },
    // 权限分配
    async assignRole(id) {
      this.roleId = id
      const { permIds } = await getRoleID(id)
      // this.permIds = permIds
      this.permIDs = permIds
      this.isShowPerm = true
    },
    currentChange(newpage) {
      // console.log(newpage)  最新当前页码
      this.pageConfig.page = newpage
      // 点击当前页从新发送请求获取数据
      this.getRoleListFn()
    },
    // 封装获取角色列表请求
    async  getRoleListFn() {
      const res = await getRoleList(this.pageConfig)
      this.roleList = res.rows
      this.total = res.total
      console.log(this.total)
    },
    // 确定按钮
    async  btnOK() {
      try {
        // 效验表单
        await this.$refs.formRole.validate()
        // 发送请求
        if (this.fromData.id) {
          // 有id就编辑
          await ediRole(this.fromData)
        } else {
          // 没id就新增
          await addRole(this.fromData)
        }
        // 关闭弹窗
        this.dialogVisible = false
        // 提示用户
        this.$message.success('操作成功')
        // 刷新页面
        this.getRoleListFn()
      } catch (error) {
        console.log(error)
      }
    },
    // 取消按钮
    async  btnDel() {
      // 重置效验
      await this.$refs.formRole.resetFields()
      // 关闭弹窗
      this.dialogVisible = false
      // 清空弹窗内容
      this.fromData = {
        name: '',
        description: ''
      }
    },
    // 编辑按钮
    async  editRole(id) {
      // console.log(id)
      // 回写数据
      this.fromData = await getRoleID(id)
      // 显示对话框
      this.dialogVisible = true
    },
    // 删除按钮
    async  delBtn(id) {
      try {
        // 二次询问
        await this.$confirm('是否确定删除？')
        // 发送请求
        await delRole(id)
        // 提示用户
        this.$message.success('删除成功')

        // 判断数据是否是数据列表最后一条数据，是的话删除最后一条页码 -1
        // 如果是在第一页就不要翻页
        if (this.roleList.length === 1 && this.pageConfig.description > 1) {
          this.pageConfig.page -= 1
        }
        // 刷新页面
        this.getRoleListFn()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>
</style>
